﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Linq.Expressions;
using VietPay.KooBoo.Data.Infrastructure;
using VietPay.KooBoo.Business.Models;
using VietPay.KooBoo.Business.Interfaces;
using VietPay.KooBoo.Data.Interfaces;
using VietPay.KooBoo.Data;
using VietPay.KooBoo.Data.EntityModels;

namespace VietPay.KooBoo.Business
{
    public class ProductService : ServiceBase<IProductModel>, IProductService
    {
        public ProductService()
            : this(new ProductRepository())
        {
        }

        private IProductRepository _repository;
        public ProductService(IProductRepository newsRepository)
        {
            _repository = newsRepository ?? new ProductRepository();
        }

        protected override Type LogPrefix
        {
            get { return this.GetType(); }
        }

        public ProductListViewModel GetProductList(int PageSize, int CurrentPage)
        {
            try
            {
                if ((CurrentPage == 0) || (PageSize == 0))
                    return null;

                IQueryable<Product> query = _repository.GetQueryable();
                ProductListViewModel model = new ProductListViewModel();
                //model.TotalItemCount = query.Count();
                //if (model.TotalItemCount == 0)
                //    return null;
                //model.TotalPageCount = (int)Math.Ceiling((double)model.TotalItemCount / (double)PageSize);
                //if (model.TotalPageCount != 1)
                //    query = query.OrderBy(x => x.DocumentName).Skip((CurrentPage - 1) * PageSize).Take(PageSize);

                //model.ThisPageItemCount = query.Count();

                model.ProductList= new List<ProductViewModel>();
                AutoMapper.Mapper.CreateMap<Product, ProductViewModel>()
                    .ForMember(dest => dest.ProductID, opt => opt.MapFrom(src => src.ProductID))
                    .ForMember(dest => dest.CategoryID, opt => opt.MapFrom(src => src.CategoryID))
                    .ForMember(dest => dest.ProductCode, opt => opt.MapFrom(src => src.ProductCode))
                    .ForMember(dest => dest.ProductName, opt => opt.MapFrom(src => src.ProductName))
                    .ForMember(dest => dest.Description, opt => opt.MapFrom(src => src.Description))
                    .ForMember(dest => dest.UserCreate, opt => opt.MapFrom(src => src.UserCreate))
                    .ForMember(dest => dest.ProductValue, opt => opt.MapFrom(src => src.ProductValue));
                model.ProductList = AutoMapper.Mapper.Map(query.ToList(), model.ProductList);

                return model;
            }
            catch (System.Exception e)
            {
                this.LogError("Error getting the Product list", e);
                return null;
            }
        }


        public ProductViewModel GetSingle(Expression<Func<ProductViewModel, bool>> whereCondition)
        {
            throw new NotImplementedException();
        }

        public void Add(ProductViewModel vModel)
        {
            Product entity=new Product();

            entity.CategoryID = vModel.CategoryID;
            entity.ProductCode= vModel.ProductCode;
            entity.ProductName= vModel.ProductName;
            entity.ProductValue= vModel.ProductValue;
            entity.UserCreate= vModel.UserCreate;
            entity.Description= vModel.Description;
            _repository.Add(entity);
        }

        public void Delete(ProductViewModel vModel)
        {
            Product news = _repository.GetAll(p => p.ProductID== vModel.ProductID).FirstOrDefault();
            _repository.Delete(news);
        }

        public void Update(ProductViewModel vModel)
        {
            Product entity = _repository.GetAll(p => p.ProductID == vModel.ProductID).FirstOrDefault();

            entity.CategoryID = vModel.CategoryID;
            entity.ProductCode = vModel.ProductCode;
            entity.ProductName = vModel.ProductName;
            entity.ProductValue = vModel.ProductValue;
            entity.UserCreate = vModel.UserCreate;
            entity.Description = vModel.Description;
            _repository.Attach(entity);
        }

        public IList<ProductViewModel> GetAll(Expression<Func<ProductViewModel, bool>> whereCondition)
        {
            throw new NotImplementedException();
        }

        public IList<ProductViewModel> GetAll()
        {
            throw new NotImplementedException();
        }

        public IQueryable<ProductViewModel> Query(Expression<Func<ProductViewModel, bool>> whereCondition)
        {
            List<ProductViewModel> newsQuery = new List<ProductViewModel>();
            IQueryable<Product> query = _repository.GetQueryable();
            AutoMapper.Mapper.CreateMap<Product, ProductViewModel>()
                .ForMember(dest => dest.ProductID, opt => opt.MapFrom(src => src.ProductID))
                    .ForMember(dest => dest.CategoryID, opt => opt.MapFrom(src => src.CategoryID))
                    .ForMember(dest => dest.ProductCode, opt => opt.MapFrom(src => src.ProductCode))
                    .ForMember(dest => dest.ProductName, opt => opt.MapFrom(src => src.ProductName))
                    .ForMember(dest => dest.Description, opt => opt.MapFrom(src => src.Description))
                    .ForMember(dest => dest.UserCreate, opt => opt.MapFrom(src => src.UserCreate))
                    .ForMember(dest => dest.ProductValue, opt => opt.MapFrom(src => src.ProductValue));
            newsQuery = AutoMapper.Mapper.Map(query, newsQuery);
            return newsQuery.AsQueryable().Where(whereCondition);
        }

        public long Count(Expression<Func<ProductViewModel, bool>> whereCondition)
        {
            throw new NotImplementedException();
        }

        public long Count()
        {
            throw new NotImplementedException();
        }
    }
}
